
/**
 * CurdGrid对象
 * @since 1.0 <2015-6-8> SoChishun Added.
 * @since 2.0 <2015-6-11> SoChishun 重构为类对象
 */
var CurdGrid = {
    newInstance: function () {
        var obj = {
            /**
             * 默认设置
             * @type JSON
             * @since 1.0 2015-6-9 SoChishun Added.
             */
            config: {
                datagrid_id: '#main_grid',
                search_dialog_id: '#dlg_search',
                editform_id: '#dlg_edit',
            },
            /**
             * 表格配置
             * @type JSON
             * @since 1.0 2015-6-8 SoChishun Added.
             */
            datagrid_config: {rownumbers: true, pagination: true, pageSize: 20, singleSelect: false, checkOnSelect: true, selectOnCheck: true, onLoadError: function () {
                    alert('抱歉,没有数据!');
                }},
            /**
             * 表格设置
             * @param JSON options
             * @since 1.0 2015-6-9 SoChishun Added.
             */
            set_datagrid_config: function (options) {
                $.extend(this.datagrid_config, options);
            },
            /**
             * 查询处理方法
             * @since 1.0 2015-6-9 SoChishun Added.
             */
            search_handler: function () {
                var data = $(this.config.search_dialog_id + ' form').serializeArray();
                var praram = {};
                for (var i = 0; i < data.length; i++) {
                    praram[data[i].name] = data[i].value;
                }
                $(this.config.datagrid_id).datagrid({
                    queryParams: praram
                });
            },
            /**
             * 保存编辑表单处理方法
             * @since 1.0 2015-6-9 SoChishun Added.
             */
            save_editform_handler: function () {
                var data = $(this.config.editform_id + ' form').serializeArray();
                var param = {};
                $.each(data, function () {
                    param[this.name] = this.value;
                })
                var $form = $(this.config.editform_id + ' form');
                $form.form('submit', {
                    url: $form.attr('action'),
                    queryParams: param,
                    onLoadError: function () {
                        alert('数据提交失败!');
                    },
                    success: function (msg) {
                        alert(msg)
                        alert(JSON.stringify(msg));
                        if (msg.status) {
                            $(obj.config.datagrid_id).datagrid('reload');
                        } else {
                            alert(msg.info);
                        }
                    }
                });
            },
            /**
             * 加载编辑表单处理方法
             * @param string url
             * @since 1.0 2015-6-9 SoChishun Added.
             */
            load_editform_handler: function (url) {
                if (!url) {
                    alert('表单数据源URL未设置!');
                    return false;
                }
                var items = $(this.config.datagrid_id).datagrid('getChecked');
                if (items.length < 1) {
                    alert('没有选择任何记录!');
                    return false;
                }
                if (items.length > 1) {
                    alert('您选中了' + items.length + '条记录,将处理第一条记录');
                }
                $(this.config.editform_id + ' form').form('load', url.replace('varid', items[0].id));
                return true;
            },
            /**
             * 重置编辑表单处理方法
             * @since 1.0 2015-6-9 SoChishun Added.
             */
            reset_editform_handler: function () {
                $(this.config.editform_id + ' form')[0].reset();
            },
            /**
             * 删除记录处理方法
             * @param string url
             * @since 1.0 2015-6-9 SoChishun Added.
             */
            delete_record_handler: function (url) {
                var items = $(this.config.datagrid_id).datagrid('getChecked');
                if (items.length < 1) {
                    alert('没有选择任何记录!');
                    return;
                }
                var ids = '';
                for (var i = 0; i < items.length; i++) {
                    ids += ',' + items[i].id;
                }
                ids = ids.substring(1);
                $.get(url.replace('varid', ids), function (data) {
                    if (data.status) {
                        $(obj.config.datagrid_id).datagrid('reload');
                    } else {
                        alert(data.info);
                    }
                })
            },
            /**
             * 新增表单前置操作
             * @since 1.0 <2015-6-11> SoChishun Added.
             */
            before_addform_dialog_handler: function () {

            },
            /**
             * curd按钮点击设置
             * @since 1.0 2015-6-10 SoChishun Added.
             */
            link_click_handler: function () {
                // 查询按钮
                $("#lnk_grid_search").click(function () {
                    obj.open_search_dialog();
                    return false;
                });
                // 新增按钮
                $("#lnk_grid_add").click(function () {
                    obj.before_addform_dialog_handler();
                    obj.open_addform_dialog();
                    return false;
                });
                // 编辑按钮
                $("#lnk_grid_edit").click(function () {
                    obj.open_editform_dialog($(this).attr('href'));
                    return false;
                });
                // 删除按钮
                $("#lnk_grid_remove").click(function () {
                    obj.delete_record_handler($(this).attr('href'));
                    return false;
                });
            },
            /**
             * 初始化
             * @param JSON options
             * @since 1.0 2015-6-8 SoChishun Added.
             */
            init: function (options) {
                if (undefined != options) {
                    $.extend(this.config, options);
                }
                // 初始化grid
                if (this.config.datagrid_id && this.datagrid_config) {
                    if (!this.datagrid_config.url) {
                        this.datagrid_config.url = $(this.config.datagrid_id).data('url');
                    }
                    //TODO: 设置pagination:true的时候,会触发重复加载数据源的BUG,待解决
                    $(this.config.datagrid_id).datagrid(this.datagrid_config);
                }
                // 初始化按钮点击事件
                this.link_click_handler();
            },
            /**
             * 打开新增对话框
             * @since 1.0 <2015-6-9> SoChishun Added.
             */
            open_editform_dialog: function (url) {
                if (this.load_editform_handler(url)) {
                    this.open_addform_dialog();
                }
            },
            /**
             * 打开编辑对话框
             * @since 1.0 <2015-6-9> SoChishun Added.
             */
            open_addform_dialog: function () {
                var config = {
                    title: '编辑表单',
                    iconCls: 'icon-edit',
                    buttons: [{
                            text: '保存',
                            iconCls: 'icon-ok',
                            handler: function () {
                                obj.save_editform_handler();
                                $(obj.config.editform_id).dialog('close');
                            }
                        }, {
                            text: '重置',
                            iconCls: 'icon-undo',
                            handler: function () {
                                obj.reset_editform_handler();
                            }
                        }, {
                            text: '取消',
                            iconCls: 'icon-cancel',
                            handler: function () {
                                $(obj.config.editform_id).dialog('close');
                            }
                        }]
                };
                $(this.config.editform_id).dialog(config);
            },
            /**
             * 打开查询对话框
             * @since 1.0 <2015-6-9> SoChishun Added
             */
            open_search_dialog: function () {
                var config = {
                    title: '搜索',
                    iconCls: 'icon-search',
                    buttons: [{
                            text: '确定',
                            iconCls: 'icon-ok',
                            handler: function () {
                                obj.search_handler();
                                $(obj.config.search_dialog_id).dialog('close');
                            }
                        }, {
                            text: '重置',
                            iconCls: 'icon-undo',
                            handler: function () {
                                $(obj.config.search_dialog_id + ' form').form('reset');
                            }
                        }, {
                            text: '取消',
                            iconCls: 'icon-cancel',
                            handler: function () {
                                $(obj.config.search_dialog_id).dialog('close');
                            }
                        }]
                };
                $(this.config.search_dialog_id).dialog(config);
            },
        };
        return obj;
    }
}